package com.group4.admin.beans;

import com.group4.controller.TeacherFacade;
import com.group4.entity.Teacher;
import com.group4.entity.User;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

/**
 *
 * @author Andreas, anjonas
 */
@ManagedBean
@RequestScoped
public class AdminTeacherModelBean {
    
    private User newUser;
    private String editId;
    
    @EJB
    private TeacherFacade teacherDb;

    /** Creates a new instance of AdminTeacherModelBean */
    public AdminTeacherModelBean() {
        newUser = new User();
    }
    
    public List<Teacher> getTeachers() {
        return teacherDb.findAll();
    }

    public User getNewUser() {
        return newUser;
    }

    public void setNewUser(User newUser) {
        this.newUser = newUser;
    }

    public String getEditId() {
        return editId;
    }

    public void setEditId(String editId) {
        this.editId = editId;
    }
    
    // deletes a teacher
    public void deleteTeacher(Teacher t) {
        teacherDb.remove(t);
    }
    
    // add a teacher
    public void addNewTeacher() {
        teacherDb.create(new Teacher(newUser));
        newUser = new User();
    }
    
    // edits a teacher
    public void editTeacher() {
        Teacher t = teacherDb.find(Long.valueOf(editId));
        t.setUser(newUser);
        teacherDb.edit(t);
        editId = null;
        newUser = new User();
    }

}
